import com.match.SqlParse;
import com.util.Error;
import com.util.Session;

import java.util.Scanner;

/**
 * Created by TimeLiar on 2015/9/22.
 * Main Frame
 */
public class UserIDEInterface {
    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        try {
            Class.forName("com.util.Constants");
            Class.forName("com.persistence.DataPersistence");
            Class.forName("com.persistence.TablePersistence");
            Class.forName("com.persistence.UserPersistence");
        } catch (ClassNotFoundException e) {
            System.out.printf("[Error]:%s\n", e.getMessage());
            System.exit(0);
        }
        System.out.printf("%s:", "Enter Username");
        String username = scanner.nextLine();
        System.out.printf("%s:", "Enter Password");
        String password = scanner.nextLine();
        try {
            Session.login(username, String.valueOf(password));
            System.out.printf("%s\n", welcome());
            boolean append = false;
            StringBuilder sql = new StringBuilder();
            while (true) {
                System.out.printf(!append ? "SQL > " : "   -> ");
                String cmd = scanner.nextLine();
                switch (cmd.toLowerCase()) {
                    case "\\e":
                    case "\\q":
                    case "quit":
                    case "exit": {
                        if (!append) {
                            System.out.printf("%s\n", "Bye!");
                            return;
                        }
                    }
                    case "\\h":
                    case "\\?":
                    case "help": {
                        if (!append) {
                            System.out.printf("%s\n", help());
                            break;
                        }
                    }
                    default: {
                        sql.append(cmd).append(" ");
                        if (!cmd.endsWith(";")) {
                            append = true;
                        } else {
                            try {
                                SqlParse parse = new SqlParse(sql.toString());
                                parse.parse();
                            } catch (Exception e) {
                                System.out.printf("%s\n", e.getMessage() == null ?
                                                "You have an error in your SQL syntax;check the manual for the right syntax" :
                                                e.getMessage()
                                );
                            }
                            sql.delete(0, sql.length());
                            append = false;
                        }
                        break;
                    }
                }
            }
        } catch (Error e) {
            System.out.printf("%s\n", e.getMessage());
        }
    }

    public static String welcome() {
        return "Welcome to the DBMS Simulator.  Commands end with ;\n" +
                "Server version: 1.0.0\n" +
                "\n" +
                "Copyright (c) 2015, TimeLiar and/or his affiliates. All rights reserved.\n" +
                "\n" +
                "Type 'help;' or '\\h' for help. Type 'exit' to exit.\n" +
                "\n";
    }

    public static String help() {
        return "List of all commands:\n" +
                "Note that all text commands must be first on line and not end with ';'\n" +
                "help      (\\?) Synonym for `help'.\n" +
                "exit      (\\e) Clear the current input statement.\n";
    }
}



